'''
Created on 2009-10-25

@author: summit
'''

import wx


try:
    from agw import foldpanelbar as fpb
except ImportError: # if it's not there locally, try the wxPython lib.
    import wx.lib.agw.foldpanelbar as fpb
    

from jolly.visual.vtkPythonViewImage2D import *

class PythonViewImage2DModel(object):
    def __init__(self, view=None, collection=None, frame=None):
        self._view = view
        self._collection = collection
        self._frame = frame
        self._syncsetting = True
    
    def SetSyncSetting(self, arg):
        self._syncsetting = arg
    
    def GetSyncSetting(self):
        return self._syncsetting
    
    def SetShowAnnotations(self, arg):
        
        if self._view:
            self._view.setShowAnnotations(arg)
            if self._collection and self._syncsetting:
                    self._collection.SyncSetShowAnnotations(arg)
            self.Update()
    
    def GetShowAnnotations(self):
        if self._view:
            return self._view.getShowAnnotations()
        return False
    
    def SetShowScalarbar(self, arg):
        if self._view:
            self._view.setShowScalarBar(arg)
            if self._collection and self._syncsetting:
                    self._collection.SyncSetShowScalarBar(arg)
            self.Update()
    
    def GetShowScalarbar(self):
        if self._view:
            return self._view.getShowScalarBar()
        return False
    
    def SetSliceOrientation(self, arg):
        if self._view:
            self._view.SetSliceOrientation(arg)
            if self._collection and self._syncsetting:
                    self._collection.SyncSetSliceOrientation(arg)
            self.Update()
    
    def GetSliceOrientation(self):
        if self._view:
            return self._view.GetSliceOrientation()
        return 0
    
    
    def SetColorLevel(self, arg):
        if self._view:
            try:
                self._view.SetColorLevel(float(arg))
                if self._collection and self._syncsetting:
                    self._collection.SyncSetColorLevel(float(arg))
                self.Update()
            except:
                pass
    
    def GetColorLevel(self):
        if self._view:
            return self._view.GetColorLevel()
        return 0.0
    
    def SetColorWindow(self, arg):
        if self._view:
            try:
                self._view.SetColorWindow(float(arg))
                if self._collection and self._syncsetting:
                    self._collection.SyncSetColorWindow(float(arg))
                self.Update()
            except:
                pass
    
    def GetColorWindow(self):
        if self._view:
            return self._view.GetColorWindow()
        return 0.0
    
    def SetCurrentSlice(self, arg):
        if self._view:
            try:
                self._view.SetSlice(int(arg))
                if self._collection:
                    self._collection.SyncSetSlice(int(arg))
                self.Update()
            except:
                pass
    
    def GetCurrentSlice(self):
        if self._view:
            return self._view.GetSlice()
        return 0
    
    def SetBackground(self, arg):
        if self._view:
            self._view.SetBackground(arg[0]/255.0, arg[1]/255.0, arg[2]/255.0)
            if self._collection and self._syncsetting:
                self._collection.SyncSetBackground(arg[0]/255.0, arg[1]/255.0, arg[2]/255.0)
            self.Update()
    
    def GetBackground(self):
        if self._view:
            return self._view.GetBackground()
        return [0,0,0]
            
    
    def Update(self):
        if self._frame.IsShownOnScreen():   # checked the main frame show in screen now ? otherwise, render winodw will fly from frame
            if self._view:
                self._view.Update()
                self._view.Render()
            if self._collection:
                self._collection.SyncRender()


    
class PythonViewImage2DPanel(wx.Panel):
    '''
    
    '''
    
    def __init__(self, parent, id, model=None, pos=wx.DefaultPosition, size=wx.DefaultSize,
                 style=wx.NO_BORDER | wx.TAB_TRAVERSAL):
        '''
        '''
        wx.Panel.__init__(self, parent, id, pos, size, style)
        
        self.model = model
        self.CreateControls()
        self.UpdateControl()
        self.GetSizer().Fit(self)
        self.GetSizer().SetSizeHints(self)
        self.GetSizer().Layout()
        
        self.Bind(fpb.EVT_CAPTIONBAR, self.OnPressCaption)
        
    
    def OnPressCaption(self, event):
        event.Skip()
    
    def CreateControls(self):
        sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(sizer)
        
        subpanel = wx.Panel(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize,
                            wx.NO_BORDER | wx.TAB_TRAVERSAL)
        sizer.Add(subpanel, 1, wx.GROW | wx.ADJUST_MINSIZE, 5)
        
        subsizer = wx.GridBagSizer(5, 5)
        subpanel.SetSizer(subsizer)
        
        # row
        self.cb3 = wx.CheckBox(subpanel, -1, "")
        subsizer.Add(wx.StaticText(subpanel,-1, "Sync Setting"), (0,0), flag=wx.ALIGN_CENTER | wx.ALL)
        subsizer.Add(self.cb3, (0,1))
        self.Bind(wx.EVT_CHECKBOX, self.OnCheckedSyncSetting, self.cb3)
        # row 1
        self.cb1 = wx.CheckBox(subpanel, -1, "")
        subsizer.Add(wx.StaticText(subpanel,-1, "Show annotation"), (1,0), flag=wx.ALIGN_CENTER | wx.ALL)
        subsizer.Add(self.cb1, (1,1))
        self.Bind(wx.EVT_CHECKBOX, self.OnCheckedShowAnnotation, self.cb1)
        
        # row2
        self.cb2 = wx.CheckBox(subpanel, -1, "")
        subsizer.Add(wx.StaticText(subpanel,-1, "Show scalar bar"), (2,0), flag=wx.ALIGN_CENTER | wx.ALL)
        subsizer.Add(self.cb2, (2,1))
        self.Bind(wx.EVT_CHECKBOX, self.OnCheckedShowScalarbar, self.cb2)
        
        # row3     
        self.ch1 = wx.Choice(subpanel, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, ["yz", "xz", "xy"], 0 )
        subsizer.Add(wx.StaticText(subpanel,-1, "Slice orientation"), (3,0), flag=wx.ALIGN_CENTER | wx.ALL)
        subsizer.Add(self.ch1, (3,1),(1,3), wx.EXPAND)
        self.Bind(wx.EVT_CHOICE, self.OnChoicedOrientation, self.ch1)
        
        # row4
        self.ed1 = wx.TextCtrl(subpanel, -1, "")
        subsizer.Add(wx.StaticText(subpanel,-1, "Color level"), (4,0), flag=wx.ALIGN_CENTER | wx.ALL)
        subsizer.Add(self.ed1, (4,1),(1,3), wx.EXPAND) # GrowableCol is 3
        self.Bind(wx.EVT_TEXT, self.OnChangedColorLevel, self.ed1)
        
        # row5
        self.ed2 = wx.TextCtrl(subpanel, -1, "")
        subsizer.Add(wx.StaticText(subpanel,-1, "Color window"), (5,0), flag=wx.ALIGN_CENTER | wx.ALL)
        subsizer.Add(self.ed2, (5,1),(1,3), wx.EXPAND) # GrowableCol is 3
        self.Bind(wx.EVT_TEXT, self.OnChangedColorWindow, self.ed2)
        
        # row6
        self.ed3 = wx.TextCtrl(subpanel, -1, "")
        subsizer.Add(wx.StaticText(subpanel,-1, "Current slice"), (6,0), flag=wx.ALIGN_CENTER | wx.ALL)
        subsizer.Add(self.ed3, (6,1),(1,3), wx.EXPAND) # GrowableCol is 3
        self.Bind(wx.EVT_TEXT, self.OnChangedCurrentSlice, self.ed3)
        
        # row7
        self.csel1 = wx.ColourPickerCtrl(subpanel, -1)
        subsizer.Add(wx.StaticText(subpanel,-1, "Background"), (7,0), flag=wx.ALIGN_CENTER | wx.ALL)
        subsizer.Add(self.csel1, (7,1),(1,1), wx.EXPAND)
        self.Bind(wx.EVT_COLOURPICKER_CHANGED, self.OnChangedBackground, self.csel1)
        
        
        subsizer.AddGrowableCol(3)
        subsizer.AddGrowableRow(3)
    
    def OnCheckedShowAnnotation(self, event):
        if self.model:
            self.model.SetShowAnnotations(event.IsChecked())
    
    def OnCheckedShowScalarbar(self, event):
        if self.model:
            self.model.SetShowScalarbar(event.IsChecked())
    
    def OnCheckedSyncSetting(self, event):
        if self.model:
            self.model.SetSyncSetting(event.IsChecked())
    
    def OnChoicedOrientation(self, event):
        if self.model:
            self.model.SetSliceOrientation(event.GetInt())
    
    def OnChangedColorLevel(self, event):
        if self.model:
            self.model.SetColorLevel(event.GetString())
    
    def OnChangedColorWindow(self, event):
        if self.model:
            self.model.SetColorWindow(event.GetString())
    
    def OnChangedCurrentSlice(self, event):
        if self.model:
            self.model.SetCurrentSlice(event.GetString())
    
    def OnChangedBackground(self, event):
        obj = event.GetEventObject()
        colour = event.GetColour()
       
        if self.model:
            self.model.SetBackground(colour)
    
    def UpdateControl(self):
        if self.model:
            self.cb1.SetValue(self.model.GetShowAnnotations())
            self.cb2.SetValue(self.model.GetShowScalarbar())
            self.cb3.SetValue(self.model.GetSyncSetting())
            self.ch1.SetSelection(self.model.GetSliceOrientation())
            self.ed1.SetValue(str(self.model.GetColorLevel()))
            self.ed2.SetValue(str(self.model.GetColorWindow()))
            self.ed3.SetValue(str(self.model.GetCurrentSlice()))
            bk = self.model.GetBackground()
        self.csel1.SetColour((bk[0]*255,bk[1]*255,bk[2]*255,255))
        
    

                   
if __name__=="__main__":
    class Collapsed(wx.Frame):
        def __init__(self, parent, id=wx.ID_ANY, title="", pos=wx.DefaultPosition,
                 size=(400,300), style=wx.DEFAULT_FRAME_STYLE):

                 wx.Frame.__init__(self, parent, id, title, pos, size, style)
                 
                 self.CreateFoldBar()
                 
        def CreateFoldBar(self, vertical=True):
            if vertical:
                self.SetSize((500,600))
            else:
                self.SetSize((700,300))
            newstyle = (vertical and [fpb.FPB_VERTICAL] or [fpb.FPB_HORIZONTAL])[0]
            bar = fpb.FoldPanelBar(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize,
                           fpb.FPB_DEFAULT_STYLE | newstyle, fpb.FPB_COLLAPSE_TO_BOTTOM)
            item = bar.AddFoldPanel("Edit View2D properties", collapsed=False)
            testpanel = PythonViewImage2DPanel(item, wx.ID_ANY, PythonViewImage2DModel(vtkPythonViewImage2D()))
            bar.AddFoldPanelWindow(item, testpanel, fpb.FPB_ALIGN_WIDTH, 0, 20)
            
            if hasattr(self, "pnl"):
                self.pnl.Destroy()

            self.pnl = bar
    
            size = self.GetClientSize()
            self.pnl.SetDimensions(0, 0, size.GetWidth(), size.GetHeight())
            
    
    
    
    app = wx.PySimpleApp()
    frame = Collapsed(None)
    frame.Show(True)
    app.MainLoop()
        